/*
 * File    : /part1/model.user.js
 * Time    : 2021-03-21 19:18:09
 * Author  : Gray Snail
 * Version : 1.0
 * Contact : shtao2011@sina.com
 * License : Copyright (c) 2021 Gray Snail
 * Desc    : None
 */

const { Sequelize, DataTypes, Model } = require('sequelize')

const sequelize = new Sequelize('sqlite::memory:')

// const User = sequelize.define('User', {
//     firstName: {
//         type: DataTypes.STRING,
//         allowNull: false,
//     },
//     lastName: {
//         type: DataTypes.STRING,
//     },
// })
class User extends Model {}

User.init(
    {
        firstName: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        lastName: {
            type: DataTypes.STRING,
        },
    },
    {
        sequelize,
        modelName: 'User',
        tableName: 'user',
        // 是否自动添加 createAt，updateAt
        timestamps: false,
    },
)

console.log(User == sequelize.models.User)

User.sync({ force: true }).then((res) => {
    console.log(res)
})

// createAt, updateAt
class Foo extends Model {
    str() {
        return [this.name, this.age, this.money].join('--')
    }
}
Foo.init(
    {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            comment: 'id',
            autoIncrement: true,
            field: 'fid',
        },
        name: {
            type: DataTypes.STRING(32),
            allowNull: false,
        },
        age: DataTypes.INTEGER,

        money: {
            type: DataTypes.NUMBER,
            defaultValue: 100,
        },
        lastOp: {
            type: DataTypes.DATE,
            defaultValue: DataTypes.NOW,
        },
        abc: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV1,
        },
    },
    {
        sequelize,
        timestamps: true,
        createdAt: false,
        updatedAt: 'update_time',
    },
)

Foo.sync({ force: true }).then((res) => {
    console.log(res)
})

const foo = Foo.build({ name: 'Bar', age: 10, money: 98.1 })
console.log(foo.str())
